package org.shj.springboot.controller;

import java.util.List;
import java.util.Map;

import org.shj.springboot.dao.BlogRepository;
import org.shj.springboot.entity.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 注解为@Contoller 时，若方法返回的是字符串，则会自动根据property里spring.mvc.view.prefix和spring.mvc.view.suffix
 * 的设置，找到相应的页面。
 * 注解为@RestController时，若方法返回的是字符串，则只会显示一个简单的页面，内容为返回的字符串的内容，想要返回一个页面，
 * 则方法必须返回 ModelAndView。
 *
 */
@Controller
@RequestMapping("/blog")
public class BlogController {

	@Autowired
	private BlogRepository blogRepository;
	
	
	@RequestMapping(value="/")
	public String showBlogList(Map<String, Object> model){
			
		List<Blog> list = blogRepository.findAll();
		model.put("searchResults", list);
		return "blog/searchBlog";
	}
	
	@RequestMapping(value="/search/{id}")
	public String viewBlog(@PathVariable String id, Map<String, Object> model){
//		Query query = new Query();
		//query.addCriteria(criteriaDefinition)
		Blog blog = blogRepository.findOne(id);
		model.put("blog", blog);
		
		return "blog/viewBlog";		
	}
	
	/*@RequestMapping(value="/search/{id}")
	public ModelAndView viewBlog(@PathVariable String id, Model model){
//		Query query = new Query();
		//query.addCriteria(criteriaDefinition)
		Blog blog = blogRepository.findOne(id);
		model.addAttribute("blog", blog);
		
		ModelAndView mav = new ModelAndView("viewBlog");
		mav.addObject(model);
		return mav;		
	}*/
	
}
